package com.example.aaa_viewpager;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SimpleCursorAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.example.aaa_viewpager.entity.UserCustom;
import com.example.aaa_viewpager.service.UserCustomDao;
import com.example.bb_serch.R;

public class fragment3 extends Fragment
{
	//UI
	private ListView listView;
	private int seletedIndex;// 使用者選擇的列
	
	//data aceess
	private UserCustomDao userCustomDao;
	private ArrayList<HashMap> favorite_list;
	
	//data
	private List<UserCustom> custom_list;
	//private UserCustom userCustom;
	
	@Override
	public void onActivityCreated(@Nullable Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onActivityCreated(savedInstanceState);
		initView();
		initHandler();
	}
	@Override
	public View onCreateView(LayoutInflater inflater,
			@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) 
	{
		
		return inflater.inflate(R.layout.view3, container, false);
    }
	private void initHandler() 
	{
		////選取的項目長按...刪除
		
		listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {

			@Override
			public boolean onItemLongClick(AdapterView<?> arg0, View view,
					 int position, long id) 
			{
				seletedIndex = position;
				AlertDialog.Builder dialog = new AlertDialog.Builder(getActivity());
				dialog.setTitle("確定要刪除...");
				dialog.setPositiveButton("確定", new DialogInterface.OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int which)
					{
						
						UserCustom u = userCustomDao.getUserById(seletedIndex);
						int id = u.getId();
						userCustomDao.delete(id);
						
						//Update ListView
						refreshListView();
						
					}
				});
				dialog.setNegativeButton("取消", new DialogInterface.OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int which)
					{
						
						
					}
				});
				dialog.show();
				return true;
			}
		});
		
		///輕觸就觸發的事件...跳到下頁(民宿記事)
		toNotePage();
	}
	private void toNotePage() 
	{
		listView.setOnItemClickListener(new ListView.OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position,
					long id)
			{
				
				//bug...刪除中間的項目 在執行會閃退
				//串聯寫法   userCustom的getName()
				String selectedText = userCustomDao.getUserById(position).getName();
				
//				int listView_id;
//				listView_id = (int) listView.getItemIdAtPosition(position)-1;
				Intent it = new Intent();
				Bundle bundle = new Bundle();
				bundle.putString("BB", selectedText);
				//bundle.putInt("BB1", listView_id);
				it.putExtras(bundle);
				it.setClass(getActivity(), BB_notes.class);
				startActivity(it);
				
			}
		});
		
	}
	private void initView() 
	{
		
		listView = (ListView) getActivity().findViewById(R.id.listView9); 
		userCustomDao = new UserCustomDao(getActivity());
		
//    	userCustomDao = new UserCustomDao(getActivity()); 
//		custom_list = userCustomDao.getAll();
//		listView.setAdapter(new ArrayAdapter(getActivity(), android.R.layout.simple_list_item_1, custom_list));
		
	
	}
	private void refreshListView() 
	{
//		Cursor cursor =  userCustomDao.getAllNotesCursor();
//		String[] from = {"NAME","ADDRESS"};
//		int[] to = {R.id.name,R.id.address};
//		SimpleCursorAdapter adapter = new SimpleCursorAdapter(getActivity(), R.layout.item_n, cursor, from, to, 0);
//		listView.setAdapter(adapter);
		
		//baseAdapter
		favorite_list = userCustomDao.getAll();
		MyAdapter adapter = new MyAdapter(getActivity(), favorite_list);
		listView.setAdapter(adapter);
		
		
	}
	@Override
	public void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		refreshListView();
		
	}
	
	
}